package com.flyshop.module.user.service;

import com.flyshop.common.utils.BCryptUtil;
import com.flyshop.common.utils.IpUtils;
import com.flyshop.common.utils.UserSessionUtil;
import com.flyshop.core.entity.DataVo;
import com.flyshop.module.user.dao.UserDao;
import com.flyshop.module.user.model.User;
import com.flyshop.module.webconfig.service.WebConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Date;

@Service
public class UserService {
    @Autowired
    private UserDao dao;
    @Autowired
    private WebConfigService configService;

    // ///////////////////////////////
    // /////       增加       ////////
    // ///////////////////////////////
    /**
     * 用户注册基本信息
     *
     * @param username
     *        用户注册登录名
     * @param password
     *        用户注册密码
     * @param request
     * @throws Exception
     */
    @Transactional
    public DataVo addUserReg(String username, String password, HttpServletRequest request) throws Exception {
        User checkuser=this.findByUsername(username);
        if (checkuser != null) {
            return DataVo.failure("该用户已存在，请换其他账户！");
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(BCryptUtil.hashpw(password, BCryptUtil.gensalt()));
        user.setTime(new Date());
        //添加用户账号和密码
        int userId = dao.addUser(user);
        //添加用户组权限
        //dao.addUserAndRole(userId,Integer.parseInt(configService.getStringByKey("user_role")));
        User userinfo = dao.findUserById(userId,0);
        UserSessionUtil.setLoginMember(request,userinfo);
        return DataVo.success("操作成功");
    }

    // ///////////////////////////////
    // /////        刪除      ////////
    // ///////////////////////////////

    // ///////////////////////////////
    // /////        修改      ////////
    // ///////////////////////////////
    /**
     * 用户登陆
     *
     * @param username
     *        用户名
     * @param password
     *        密码
     * @param request
     * @throws Exception
     */
    @Transactional
    public User userLogin(String username, String password, HttpServletRequest request) {
        User user = dao.findByUsername(username);
        if (user != null) {
            User login = new User();
            if (BCryptUtil.checkpw(password, user.getPassword())) {
                login.setUserId(user.getUserId());
                login.setAttempts(0);
                login.setLastLogin(new Date());
                login.setLoginIp(IpUtils.getIpAddr(request));
                dao.updateUserLogin(login);
                //用户信息写入session
                UserSessionUtil.setLoginMember(request,user);
            }else{
                login.setUserId(user.getUserId());
                login.setAttempts(user.getAttempts()+1);
                login.setAttemptsTime(new Date());
                dao.updateUserLogin(login);
                user = null;
            }
        }
        return user;
    }





    // ///////////////////////////////
    // /////        查询      ////////
    // ///////////////////////////////
    /**
     * 通过userId查询用户信息
     * 用户状态0是所有 1未审核 2正常状态 3 删除至回收站 4锁定
     *
     * @param userId
     *         用户id
     * @param  status
     *          审核状态
     * @return User
     */
    public User findUserById(int userId,int status) {
        return dao.findUserById(userId,status);
    }

    /**
     * 通过username查询用户信息
     *
     * @param username
     * @return User
     */
    public User findByUsername(String username) {
        return dao.findByUsername(username);
    }
}
